package com.leonyr.smartipaddemo.crop;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.leonyr.smartipaddemo.R;

import java.util.ArrayList;
import java.util.List;

public class ConShutterFragment extends DialogFragment implements View.OnClickListener{
    private TextView tv_sheet3;
    private TextView tv_sheet5;
    private TextView tv_sheet10;
    private TextView tv_interval05;
    private TextView tv_interval10;
    private TextView tv_interval15;
    private EditText et_sheet;
    private EditText et_interval;
    private Button btn_ensure;
    private Button btn_cancel;
    private List<View> sheetList = new ArrayList<>();
    private List<View> intervalList = new ArrayList<>();
    private OnConSetListener listener;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listener = (OnConSetListener) context;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定

        dialog.setCanceledOnTouchOutside(false);
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.dimAmount = 0.6f;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fm_con_shutter, container,false);
        iniData();
        initView(view);
        return view;
    }

    private void initView(View view) {
        tv_interval05 = view.findViewById(R.id.tv_interval05);
        tv_interval05.setOnClickListener(this);
        tv_interval10 = view.findViewById(R.id.tv_interval10);
        tv_interval10.setOnClickListener(this);
        tv_interval15 = view.findViewById(R.id.tv_interval15);
        tv_interval15.setOnClickListener(this);
        intervalList.add(tv_interval05);
        intervalList.add(tv_interval10);
        intervalList.add(tv_interval15);
        tv_sheet3 = view.findViewById(R.id.tv_sheet3);
        tv_sheet3.setOnClickListener(this);
        tv_sheet5 = view.findViewById(R.id.tv_sheet5);
        tv_sheet5.setOnClickListener(this);
        tv_sheet10 = view.findViewById(R.id.tv_sheet10);
        tv_sheet10.setOnClickListener(this);
        sheetList.add(tv_sheet3);
        sheetList.add(tv_sheet5);
        sheetList.add(tv_sheet10);
        et_interval = view.findViewById(R.id.et_interval);
        et_sheet = view.findViewById(R.id.et_sheet);
        btn_ensure = view.findViewById(R.id.btn_ensure);
        btn_ensure.setOnClickListener(this);
        btn_cancel = view.findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(this);

    }

    private void iniData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_sheet3:
            case R.id.tv_sheet5:
            case R.id.tv_sheet10:
                setSheetStates(view);
                break;
            case R.id.tv_interval05:
            case R.id.tv_interval10:
            case R.id.tv_interval15:
                setIntervalStates(view);
                break;
            case R.id.btn_ensure:
                int sheet = 1;
                float interval = 1;
                String sheetStr = et_sheet.getText().toString().trim();
                String intervalStr = et_interval.getText().toString().trim();
                if(!TextUtils.isEmpty(sheetStr)){
                    sheet = Integer.parseInt(sheetStr);
                }
                if(!TextUtils.isEmpty(intervalStr)){
                    interval = Float.parseFloat(intervalStr);
                }
                listener.onConSet(sheet, (long) (interval*1000));
                dismiss();
                break;
            case R.id.btn_cancel:
                //
                dismiss();
                break;
        }
    }

    private void setSheetStates(View view){
        for(View v:sheetList){
            if(v==view){
                if(view.isSelected()){
                    view.setSelected(false);
                    et_sheet.setText("");
                }else{
                    view.setSelected(true);
                    switch (view.getId()){
                        case R.id.tv_sheet3:
                            et_sheet.setText("3");
                            break;
                        case R.id.tv_sheet5:
                            et_sheet.setText("5");
                            break;
                        case R.id.tv_sheet10:
                            et_sheet.setText("10");
                            break;
                    }
                }
            }else{
                v.setSelected(false);
            }
        }

    }

    private void setIntervalStates(View view){
        for(View v:intervalList){
            if(v==view){
                if(view.isSelected()){
                    view.setSelected(false);
                    et_interval.setText("");
                }else{
                    view.setSelected(true);
                    switch (view.getId()){
                        case R.id.tv_interval05:
                            et_interval.setText("0.5");
                            break;
                        case R.id.tv_interval10:
                            et_interval.setText("1");
                            break;
                        case R.id.tv_interval15:
                            et_interval.setText("1.5");
                            break;
                    }
                }
            }else{
                v.setSelected(false);
            }
        }
    }

    public interface OnConSetListener{
        void onConSet(int sheet,long interval);
    }

}
